package com.dwy2002.enums;

import lombok.Getter;

import java.util.Arrays;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/03/11
 */
@Getter
public enum OrderTypeEnum {
    PRODUCT(1, "productOrderHandler"),
    COURSE(2, "courseOrderHandler"),
    ROOM(3, "roomOrderHandler");

    private final int code;
    private final String handlerName;

    OrderTypeEnum(int code, String handlerName) {
        this.code = code;
        this.handlerName = handlerName;
    }

    /**
     * 根据订单类型返回实现类
     *
     * @param code 订单类型
     * @return 1：productOrderHandler 2：courseOrderHandler 3：roomOrderHandler
     * @author 杜伟毅
     * @since 2025/3/11 0011 23:11
     */
    public static String getHandlerNameByCode(int code) {
        return Arrays.stream(values())
                .filter(type -> type.code == code)
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("无效的订单类型"))
                .handlerName;
    }

    /**
     * 检测是否是合法订单类型
     *
     * @param value 订单类型数字
     * @return true:合法 false:不合法
     * @author 杜伟毅
     * @since 2025/3/11 0011 23:15
     */
    public static boolean isValidOrderTypeEnum(int value) {
        for (OrderTypeEnum orderTypeEnum : OrderTypeEnum.values()) {
            if (value == orderTypeEnum.code) {
                return true;
            }
        }
        return false;
    }
}
